package com.press.controllers.blog;

import com.press.bean.ContentBean;
import com.press.bean.TaxonomyBean;
import com.press.beanforview.VContentBean;
import com.press.beanforview.VMenuBean;
import com.press.services.DetailsService;
import com.press.services.UtilService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import sun.misc.VM;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by haocongshun on 2016/10/21.
 */
@Controller("utilController")
public class UtilController {
    private static Logger log = Logger.getLogger(UtilController.class);
    @Resource(name = "utilService")
    private UtilService utilService;

    public UtilService getUtilService() {
        return utilService;
    }

    public void setUtilService(UtilService utilService) {
        this.utilService = utilService;
    }

    public void contentsTop10(HttpServletRequest request){
        List<VContentBean> contentsTop10 = new ArrayList<VContentBean>();
        for (ContentBean content :utilService.getContentsTop10()){
            VContentBean vContent = new VContentBean();
            vContent.setTitle(content.getContent_title());
            vContent.setUrl(request.getServletContext().getAttribute("URLContent")+"?content_id="+content.getContent_id());
            contentsTop10.add(vContent);
        }
        //TODO:将top10存入request中
        request.setAttribute("contentsTop10",contentsTop10);
    }
    public void contentsRecommend(HttpServletRequest request){
        List<VContentBean> contentsRecommend = new ArrayList<VContentBean>();
        for (ContentBean content :utilService.getContentsRecommend()){
            VContentBean vContent = new VContentBean();
            vContent.setTitle(content.getContent_title());
            vContent.setUrl(request.getServletContext().getAttribute("URLContent")+"?content_id="+content.getContent_id());
            contentsRecommend.add(vContent);
        }
        //TODO:将热recommend存入request中
        request.setAttribute("contentsRecommend",contentsRecommend);
    }
    public void menus(HttpServletRequest request){
        List<VMenuBean> menus = new ArrayList<VMenuBean>();
        for(TaxonomyBean taxonomy : utilService.getMenus()){
            VMenuBean vMenu = new VMenuBean();
            vMenu.setTitle(taxonomy.getTaxonomy_title());
            vMenu.setUrl(request.getServletContext().getAttribute("URLTaxonomy")+"?tax="+taxonomy.getTaxonomy_id());
            menus.add(vMenu);
        }
        //TODO:将热menu存入request中
        request.setAttribute("menus",menus);
    }

}
